<div class="weapp-body">

    <script type="text/html" id="toolbar">
        <div class="layui-btn-container">
            <button class="layui-btn" lay-event="download_app">
                <i class="layui-icon layui-icon-download-circle"></i>获取应用
            </button>
        </div>
    </script>

    <script type="text/html" id="opt_bar">
        <!--        <a href="javascript:;" title="删除" lay-event="delete">-->
        <!--            <i class="layui-icon">&#xe640;</i>-->
        <!--        </a>-->
    </script>

    <table id="app_table" lay-filter="app_table"></table>

</div>
<script>
    layui.extend({
        admin: '<?php echo Yii::app()->baseUrl?>/static/admin/js/admin'
    });
    layui.use(['table','jquery','admin'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var load_index = layer.load();
        table.render({
            elem: '#app_table',
            method: 'post',
            url: '<?php echo $this->createUrl('app/list')?>', //数据接口
            page: true, //开启分页
            toolbar: '#toolbar',
            id: 'app_table',
            defaultToolbar:[],
            loading:false,
            cols: [[ //表头
                // {type: 'checkbox', fixed: 'left'},
                {field: 'agentid', title: '应用id',align:'center'},
                {field: 'name', title: '应用名称',align:'center'},
                {field: 'avatar', title: 'logo',align:'center', templet:function (row) {
                        return '<img src="' + row.square_logo_url + '" width="35" height="45"/>'
                    }},
                { title: '秘钥',align:'center',edit: 'text',templet:function (row) {
                        return '*'.repeat(16);
                    }},
                // {fixed: 'right', title:'操作', toolbar: '#opt_bar',align:'center'}
            ]],
            done:function (data) {
                layer.close(load_index);
            }
        });

        table.on('toolbar(app_table)', function (obj) {
            switch (obj.event) {
                case 'download_app' :
                    var index = layer.load();
                    $.get('<?php echo $this->createUrl('app/getList')?>', function (ret) {
                        layer.close(index);
                        if (ret.code == 0) {
                            table.reload('app_table');
                            layer.msg(ret.message,{time:1000, icon:1});
                        } else {
                            layer.msg(ret.message,{time:1500, icon:2});
                        }
                    });
                    break;
                default:
                    return;
            }
        });
        //监听单元格编辑
        table.on('edit(app_table)', function(obj){
            layer.confirm('确认修改应用"' + obj.data.name+'"秘钥？', function (index) {
                layer.close(index);
                var parmas = {
                    value:obj.value,
                    agent_id:obj.data.agentid
                };
                $.post('<?php echo $this->createUrl('app/updateSecret')?>',parmas, function (ret) {
                    if (ret.code != 0) {
                        layer.msg(ret.message,{time:1500,icon:2});
                    } else {
                        layer.msg(ret.message,{time:1000,icon:1});
                        table.reload('app_table');
                    }
                });
            });
        });
    });
</script>